package impl;

import anaydis.sort.SorterType;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class HSorter extends AbstractSorter{

    public HSorter(){    //Pendiente arreglo. No funciona con la animacion
        this(SorterType.H);
    }

    protected HSorter(final SorterType type){
        super(type);
    }

    public <T> void sort(Comparator<T> comparator, List<T> list){
        sort(comparator, list, 11);
        (new InsertionSorter()).sort(comparator, list);
    }

    @NotNull
    public SorterType getType() {
        return SorterType.H;
    }

    protected <T> void sort(Comparator<T> comparator, List<T> list, int h){
        AbstractSorter sorter = new InsertionSorter();
        if(h<list.size()){
            List <T>aux;
            for(int g = 0; g<h;g++){
                aux = new ArrayList<T>();
                for(int i = g; i<(list.size()/h);i++){
                    aux.add(list.get(h*i));
                }
                sorter.sort(comparator, aux);
                for(int i = 0; i<aux.size(); i++){
                    list.set(h*(i+g), aux.get(i));
                }
            }
        }
    }
}